package com.syf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syf.cmd.SelByIDCmd;
import com.syf.cmd.SoftwareReleaseCmd;
import com.syf.constant.BsConstants;
import com.syf.model.Mails;
import com.syf.rocker.Source;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syf.domain.store.BsSoftwareRelease;
import com.syf.mapper.BsSoftwareReleaseMapper;
import com.syf.service.BsSoftwareReleaseService;
import org.springframework.util.MimeTypeUtils;

import java.util.List;

/**
*
*
*@author syf
*@create 2022-02-07 19:09
*/
@Slf4j
@Service
@CacheConfig(cacheNames = "BsSoftwareReleaseServiceImpl")
public class BsSoftwareReleaseServiceImpl extends ServiceImpl<BsSoftwareReleaseMapper, BsSoftwareRelease> implements BsSoftwareReleaseService{

    @Autowired
    private Source source;


    /**
     * 根据软件厂商ID 查询软件
     * @param id
     * @return
     */
    @Override
    public List<BsSoftwareRelease> selectSoftwareByCompanyId(Integer id) {
        LambdaQueryWrapper<BsSoftwareRelease> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id != null, BsSoftwareRelease::getCompanyId, id);
        return list(wrapper);
    }

    /**
     * 分页条件查询--应用(/发布表)列表
     * @return
     */
    @Override
    public Page<BsSoftwareRelease> findByPage(SoftwareReleaseCmd softwareReleaseCmd) {
        log.info("Serive层参数：{}", softwareReleaseCmd);
        String softwareName = softwareReleaseCmd.getSoftwareName();
        Integer whatClass = softwareReleaseCmd.getWhatClass();
        String softwareCode = softwareReleaseCmd.getSoftwareCode();
        Integer smallClass = softwareReleaseCmd.getSmallClass();
        Integer status = softwareReleaseCmd.getStatus();
        Integer releaseStatus = softwareReleaseCmd.getReleaseStatus();
        Integer isRecommend = softwareReleaseCmd.getIsRecommend();
        Integer companyId = softwareReleaseCmd.getCompanyId();
        Integer orderByType = softwareReleaseCmd.getOrderByType();
        Page<BsSoftwareRelease> page2 = new Page<>(softwareReleaseCmd.getCurrent(),softwareReleaseCmd.getSize());

        LambdaQueryWrapper<BsSoftwareRelease> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(softwareName), BsSoftwareRelease::getSoftwareName,softwareName)
                .like(StringUtils.isNotBlank(softwareCode), BsSoftwareRelease::getSoftwareCode,softwareCode)
                .eq(smallClass != null,BsSoftwareRelease::getSmallClass,smallClass)
                .eq(status != null,BsSoftwareRelease::getStatus,status)
                .eq(releaseStatus != null,BsSoftwareRelease::getReleaseStatus,releaseStatus)
                .eq(isRecommend != null,BsSoftwareRelease::getIsRecommend,isRecommend)
                .eq(companyId != null,BsSoftwareRelease::getCompanyId,companyId);

        if(whatClass != null && whatClass == 1){
            wrapper.isNotNull(BsSoftwareRelease::getSoftwareCode);
        }

        if(orderByType == null){
            wrapper.orderByDesc(BsSoftwareRelease::getCreateTime);   //默认
        }else {
            switch (orderByType){
                case 1:
                    wrapper.orderByDesc(BsSoftwareRelease::getUpdateTime);  //更新时间
                    break;
                case 2:
                    wrapper.orderByDesc(BsSoftwareRelease::getDownloadSize);  //下载量
                    break;
                case 3:
                    wrapper.orderByDesc(BsSoftwareRelease::getAvgScore);  //评分
                    break;
                case 0:
                    wrapper.orderByDesc(BsSoftwareRelease::getCreateTime);   //默认
            }
        }



        Page<BsSoftwareRelease> releasePage = page(page2, wrapper);
        return releasePage;
    }

    /**
     * 检测该软件是否已经存在
     *
     * @param softwareSign
     * @return
     */
    @Override
    public boolean checkExist(String softwareSign) {
        LambdaQueryWrapper<BsSoftwareRelease> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsSoftwareRelease::getSoftwareCode,softwareSign)
                .eq(BsSoftwareRelease::getReleaseStatus,1);
        List<BsSoftwareRelease> list = list(wrapper);
        if(list.size() > 0){
            return true;   //存在了
        }
        return false;
    }

    /**
     * 根据softwareCode查询应用
     * @return
     */
    @Override
    public BsSoftwareRelease selBycode(String softwareCode) {
        LambdaQueryWrapper<BsSoftwareRelease> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsSoftwareRelease::getSoftwareCode,softwareCode)
                .eq(BsSoftwareRelease::getReleaseStatus,1);
        return getOne(wrapper);
    }

    /**
     * 根据softwareCode查询应用
     *
     * @param softwareSign
     * @return
     */
    @Override
    public BsSoftwareRelease selBySign(String softwareSign) {
        LambdaQueryWrapper<BsSoftwareRelease> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsSoftwareRelease::getSoftwareSign,softwareSign)
                .eq(BsSoftwareRelease::getReleaseStatus,0);
        return getOne(wrapper);
    }


    /**
     * 前台--查询编辑推荐软件
     *
     * @return
     */
    @Override
    //@Cacheable(key = "'recommendList'")   //KEY:  BsSoftwareReleaseServiceImpl::recommendList
    public List<BsSoftwareRelease> recommendList() {
        LambdaQueryWrapper<BsSoftwareRelease> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNotNull(BsSoftwareRelease::getSoftwareCode)
                .eq(BsSoftwareRelease::getReleaseStatus,1) //审核通过
                .eq(BsSoftwareRelease::getStatus,0) //在线
                .eq(BsSoftwareRelease::getIsRecommend,0) //推荐
                .select(BsSoftwareRelease::getId,BsSoftwareRelease::getSoftwareName,BsSoftwareRelease::getSoftwareCode,
                        BsSoftwareRelease::getRjLogo,BsSoftwareRelease::getRjTitle,BsSoftwareRelease::getMbSize,
                        BsSoftwareRelease::getAvgScore,BsSoftwareRelease::getDownloadSize,BsSoftwareRelease::getUpdateTime)
                .last("limit "+ BsConstants.recommendSize);  // 手动把sql拼接到最后(有sql注入的风险,请谨慎使用)
        List<BsSoftwareRelease> list = list(wrapper);
        log.info("编辑推荐应用条数：{}",list.size());
        return list;
    }

    /**
     * 前台--查询 新软推荐应用
     * @return
     */
    @Override
    //@Cacheable(key = "'newSoftList'")   //KEY:  BsSoftwareReleaseServiceImpl::newSoftList
    public List<BsSoftwareRelease> newSoftList() {
        LambdaQueryWrapper<BsSoftwareRelease> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNotNull(BsSoftwareRelease::getSoftwareCode)
                .eq(BsSoftwareRelease::getReleaseStatus,1) //审核通过
                .eq(BsSoftwareRelease::getStatus,0) //在线
                .select(BsSoftwareRelease::getId,BsSoftwareRelease::getSoftwareName,BsSoftwareRelease::getSoftwareCode,
                        BsSoftwareRelease::getRjLogo,BsSoftwareRelease::getRjTitle,BsSoftwareRelease::getMbSize,
                        BsSoftwareRelease::getAvgScore,BsSoftwareRelease::getDownloadSize,BsSoftwareRelease::getCreateTime)
                .orderByDesc(BsSoftwareRelease::getCreateTime)   //按创建时间排序
                .last("limit "+ BsConstants.newSoftSize);  // 手动把sql拼接到最后(有sql注入的风险,请谨慎使用)
        List<BsSoftwareRelease> list = list(wrapper);
        log.info("新软推荐应用条数：{}",list.size());
        return list;
    }

    /**
     * 前台--查询 热门软件排行榜--应用列表：
     *        目标： 根据近三个月来的下载数量进行排行(每一次下载，都生成一条下载记录)
     *
     * @return
     */
    @Override
    //@Cacheable(key = "'hotrRankingList'")   //KEY:  BsSoftwareReleaseServiceImpl::hotrRankingList
    public List<BsSoftwareRelease> hotrRankingList() {
        LambdaQueryWrapper<BsSoftwareRelease> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNotNull(BsSoftwareRelease::getSoftwareCode)
                .eq(BsSoftwareRelease::getReleaseStatus,1) //审核通过
                .eq(BsSoftwareRelease::getStatus,0) //在线
                .select(BsSoftwareRelease::getId,BsSoftwareRelease::getSoftwareName,BsSoftwareRelease::getSoftwareCode,
                        BsSoftwareRelease::getRjLogo,BsSoftwareRelease::getRjTitle,BsSoftwareRelease::getMbSize,
                        BsSoftwareRelease::getAvgScore,BsSoftwareRelease::getDownloadSize,BsSoftwareRelease::getUpdateTime)
                .orderByDesc(BsSoftwareRelease::getDownloadSize)   //按下载次数排序
                .last("limit 10");  // 手动把sql拼接到最后(有sql注入的风险,请谨慎使用)
        List<BsSoftwareRelease> list = list(wrapper);
        log.info("热门软件排行榜应用：{}",list.toString());
        return list;
    }

    /**
     * 应用下载--发送MQ异步队列增加下载次数
     *
     * @return
     */
    @Override
    public boolean downloadSoftMQ(SelByIDCmd selByIDCmd) {
        //使用MQ异步发生邮箱给厂商
        Integer id = selByIDCmd.getId();
        MessageBuilder<Integer> mailMessageBuilder = MessageBuilder
                .withPayload(id)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
        boolean send = source.downloadOut().send(mailMessageBuilder.build());
        log.info("MQ 产生消息--下载软件：{}",id);

        return send;
    }


}
